import java.util.Arrays;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/9/18
 * Time: 12:13
 */
public class Solution {
    public int[] inventoryManagement(int[] stock, int cnt) {
        quickSort(stock,0,stock.length - 1,cnt);
        int[] ret = new int[cnt];
        System.arraycopy(stock, 0, ret, 0, cnt);
        return ret;
    }

    private void quickSort(int[] nums, int low, int high,int k) {
        int key = nums[new Random().nextInt(high - low + 1) + low],left = low - 1,right = high + 1,index = low;
        while (index < right) {
            if (nums[index] < key) {
                swap(nums, index++, ++left);
            }else if (nums[index] == key) {
                index++;
            }else {
                swap(nums, index, --right);
            }
        }
        int a = left - low + 1,b = right - left - 1;
        if (a > k) {
            quickSort(nums, low, left, k);
        }else if (a + b >= k) {
            return;
        }else {
            quickSort(nums, right, high, k - a - b);
        }
    }

    private void swap(int[] nums, int left, int right) {
        int tmp = nums[left];
        nums[left] = nums[right];
        nums[right] = tmp;
    }

    public static void main(String[] args) {
        Solution solution = new  Solution();
        int[] ret = solution.inventoryManagement(new int[]{0,0,0,2,0,5}, 0);
        System.out.println(Arrays.toString(ret));
    }
}